package com.wys.config;

import com.wys.anotations.I18nRes;
import com.wys.contants.ReqConstant;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @author NICK_WYS
 * @date 2021/1/20
 */
@Component
public class I18nResInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            final HandlerMethod handlerMethod = (HandlerMethod) handler;
            final Class<?> clazz = handlerMethod.getBeanType();
            final Method method = handlerMethod.getMethod();
            if (clazz.isAnnotationPresent(I18nRes.class)) {
                request.setAttribute(ReqConstant.RES_NEED_I18N, true);
            } else if (method.isAnnotationPresent(I18nRes.class)) {
                request.setAttribute(ReqConstant.RES_NEED_I18N, true);
            }
        }
        return true;
    }
}
